<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Notice;
use Illuminate\Support\Facades\Redis;

class NoticeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'notice:general';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '汇总当天的提醒';

    public const RedisNoticeKey = 'notice_today_key';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        Redis::del(self::RedisNoticeKey);

        $notice = Notice::query()
            ->where('status', 1)
            ->where('content', '!=', '')
            ->where(function ($query) {
                $query->where('is_delay_limit', 1)
                    ->orWhere(function ($query) {
                        $query->where('is_delay_limit', 0)->where('delay_limit', '>', 0);
                    });
            })->get();

        foreach($notice as $no) {
            if (1 == $no->delay_type) {
                // 每时
                $this->hour($no);
            } elseif (2 == $no->delay_type) {
                // 每天
                $this->day($no);
            } elseif (3 == $no->delay_type) {
                // 每周
                $this->week($no);
            } elseif (4 == $no->delay_type) {
                // 每月
                // $this->month($no);
            }
        }

        return 0;
    }

    private function hour(Notice $notice)
    {
        $now = now();
        $start = $now->toDateString() . $notice->delay_hour_minute;
        $end = $now->toDateString() . $notice->delay_hour_minute_end;
        $max = floor((strtotime($end) - strtotime($start)) / 3600); //共多少小时
        $num = 0;
        if ($notice->delay_day<=0) {
            $notice->delay_day = 1;
        }
        for ($i=0; $i < $max; $i+=$notice->delay_day) {
            if ($num > 23) {
                break;
            }
            $timestamp = strtotime($now->toDateString() . $notice->delay_hour_minute) + $i*3600;
            Redis::hSet(self::RedisNoticeKey, $timestamp . '.' . $notice->id, serialize($notice->toArray()));
            $num++;
        }
    }

    private function day(Notice $notice)
    {
        // 是否今日执行
        $isToday = 0;
        $now = now();

        if (is_numeric($notice->delay_day)) {
            if (0 == $notice->delay_day) {
                $isToday = 1;
            } else {
                if ($now->day == $notice->delay_day) {
                    $isToday = 1;
                }
            }
        } else {
            if (in_array($now->day, explode('-', $notice->delay_day))) {
                $isToday = 1;
            }
        }

        if (1 == $isToday) {
            $timestamp = strtotime($now->toDateString() . $notice->delay_hour_minute);
            Redis::hSet(self::RedisNoticeKey, $timestamp . '.' . $notice->id, serialize($notice->toArray()));
        }
    }

    private function week(Notice $notice)
    {
        // 是否今日执行
        $isToday = 0;
        $now = now();
        $week = [0=>7,1=>1,2=>2,3=>3,4=>4,5=>5,6=>6];

        if (is_numeric($notice->delay_day)) {
            if (0 == $notice->delay_day) {
                $isToday = 1;
            } else {
                if ($week[$now->dayOfWeek] == $notice->delay_day) {
                    $isToday = 1;
                }
            }
        } else {
            if (in_array($week[$now->dayOfWeek], explode('-', $notice->delay_day))) {
                $isToday = 1;
            }
        }

        if (1 == $isToday) {
            $timestamp = strtotime($now->toDateString() . $notice->delay_hour_minute);
            Redis::hSet(self::RedisNoticeKey, $timestamp . '.' . $notice->id, serialize($notice->toArray()));
        }
    }

    private function month(Notice $notice){}
}
